<?php

class commController extends coreController
{
    // 현재 페이지 내용
    private $sContents;

    // 기본 레이어 설정 및 정보
    protected $sTopLayoutView = true;
    protected $sFooterLayoutView = true;
    protected $sAsideLayoutView = false;

    // cop top layout
    protected $sTopLayout = '/layout/top.html';
    // cop footer layout
    protected $sFooterLayout = '/layout/footer.html';

    function __construct($params = array())
    {
        parent::__construct($params);
    }

    /**
     *
     * GET, POST 에 대한 문자열 필터링
     * XSS, GET, POST 문자열 검사
     *
     */
    protected function filter()
    {
        $this->params = utilFilterSecure::filterSecure(get_class($this), $this->action, $this->params);
        if (!$this->checkFilterXss()) {
            $this->params = utilFilterXss::getValidParams($this->params);
        }
    }

    protected function checkFilterXss()
    {
        return false;
    }

    public function view($sViewFile, $aAssignData = array())
    {
        // header
        commViewer::_header();

        // 데이터 세팅
        if (empty($aAssignData) === false) {
            foreach ($aAssignData as $key => $val) {
                if (empty($key)) continue;

                ${$key} = $val;
            }
        }
        
        // 기본 컨텐츠 세팅 (title, css, js)
        commViewer::loadPacking();
        
        // title
        //$title_in_template = self::$sTitle;
        $title_in_template = commViewer::getTitle();
        
        // css
        $css_in_template = commViewer::getOptimizerCss();
        
        // js
        $js_in_template = commViewer::getOptimizerJs();

        require_once(SC_DIR_VIEW.'/template/default.html');

        /*
        // top layout 세팅
        if ($this->sTopLayoutView) {
            $sTopLayout = $this->getTopLayout();
            if (file_exists($sTopLayout)) {
                // aside 여부
                $sAsideLayoutView = $this->sAsideLayoutView;
                require_once ($sTopLayout);

            } else {
                __d('[VIEW FILE NOT FOUND!!] '.$sTopLayout);
            }
        }

        // view 파일 세팅
        $sViewFilePath = SC_DIR_VIEW.'/'.$sViewFile;
        if (file_exists($sViewFilePath)) {
            require_once($sViewFilePath);
        } else {
            __d('[FILE NOT FOUND!! VIEW] : '.$sViewFilePath);
        }

        // footer layout 세팅
        if ($this->sFooterLayoutView) {
            $sFooterLayout = $this->getFooterLayout();
            if (file_exists($sFooterLayout)) {
                require_once ($sFooterLayout);

            } else {
                __d('[VIEW FILE NOT FOUND!!] '.$sFooterLayout);
            }
        }
        */

        //$this->sContents = ob_get_contents();
        
        if ( headers_sent() ){
            $encoding = false;
        
        } else if ( strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== false ) {
            $encoding = 'x-gzip';
        
        } else if ( strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip') !== false ) {
            $encoding = 'gzip';
        
        } else {
            $encoding = false;
        }
        
        //$contents1 = ob_get_contents();
        //ob_end_clean();

        //$this->_display();

        // 기본 컨텐츠 세팅 (title, css, js)
        //commViewer::loadPacking();

        // 컨텐츠 내용 세팅
        //commViewer::display($contents1);
        
        if ($encoding) {
            $contents = ob_get_contents();
            ob_end_clean();
            header('Content-Encoding: '.$encoding);
            print("\x1f\x8b\x08\x00\x00\x00\x00\x00");
            $size = strlen($contents);
            $contents = gzcompress($contents, 9);
            $contents = substr($contents, 0, $size);
            print($contents);
            //exit();
            
        } else {
            ob_end_flush();
            //exit();
        }

        return true;
    }

    /**
    *
    * layout 표시 여뷰를 설정
    * @param boolean $sTopLayoutView		TOP
    * @param boolean $sFooterLayoutView	FOOTER
    * @param boolean $sAsideLayoutView		ASIDE
    */
    public function setLayoutView($sTopLayoutView = true, $sFooterLayoutView = true, $sAsideLayoutView = true)
    {
        $this->sTopLayoutView = $sTopLayoutView;
        $this->sFooterLayoutView = $sFooterLayoutView;
        $this->sAsideLayoutView = $sAsideLayoutView;
    }

    /**
     *
     * top layout 세팅
     * @param string $sTopLayout
     * @return boolean
     */
    protected function setTopLayout($sTopLayout)
    {
        if (empty($sTopLayout)) return false;

        $this->sTopLayout = $sTopLayout;
    }

    /**
     *
     * top layout 조회
     */
    private function getTopLayout()
    {
        return SC_DIR_VIEW.$this->sTopLayout;
    }

    /**
     *
     * footer layout 세팅
     * @param string $sFooterLayout
     * @return boolean
     */
    protected function setFooterLayout($sFooterLayout)
    {
        if (empty($sFooterLayout)) return false;

        $this->sFooterLayout = $sFooterLayout;
    }

    /**
     *
     * footer layout 조회
     */
    private function getFooterLayout()
    {
        return SC_DIR_VIEW.$this->sFooterLayout;
    }
}
